package com.java.basis.thread.n3.shunxu;

import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;

/**
 * 使用 notify 和 wait解决线程的执行顺序
 * @author wangning
 * @create 2022-01-24 15:54
 */
@Slf4j(topic = "c.Test01")
public class Test01 {

	static final Object lock = new Object();
	//表示t2是否运行过
	static boolean t2Runned = false;

	public static void main(String[] args) {
		Thread t1 = new Thread(() -> {
			synchronized (lock) {
				while (!t2Runned) {
					try {
						lock.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				log.debug("1");

			}
		}, "t1");

		Thread t2 = new Thread(() -> {
			synchronized (lock) {
				t2Runned = true;
				log.debug("2");
				lock.notifyAll();
			}
		}, "t2");
		t1.start();
		t2.start();
	}

}
